﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;
using System.Collections.Generic;

namespace WebApp.Controls
{
    public partial class BrandProduct_Interest2 : System.Web.UI.UserControl
    {
        private ProductManager m_ProductManager = new ProductManager();

        private int m_ShowCount = 3;
        public int ShowCount
        {
            set { m_ShowCount = value; }
        }

        private Cosmetic.DataEntity.Product m_Product;
        public Cosmetic.DataEntity.Product Product
        {
            set { m_Product = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindProduct();
            }
        }

        private void BindProduct()
        {
            List<ProductInfo> productList = this.m_ProductManager.GetProductInfoByBrand(m_Product).OrderByDescending(theProduct => theProduct.Views).ToList();
            int theLenght = productList.Count;
            Random ran = new Random();
            int[] theNum = new int[m_ShowCount];
            int i = 0;
            while (true)
            {
                int tem = ran.Next(theLenght);
                if (!theNum.Contains(tem) && i < m_ShowCount)
                {
                    theNum[i] = tem;
                    i++;
                }
                else if (i == m_ShowCount)
                {
                    break;
                }
            }

            List<ProductInfo> showList = new List<ProductInfo>();

            for (int j = 0; j < m_ShowCount; j++)
            {
                showList.Add(productList[theNum[j]]);
            }

            this.lvProduct.DataSource = showList; //this.m_ProductManager.GetProductInfoByBrand(m_Product).OrderByDescending(theProduct => theProduct.Views).Take(m_ShowCount).ToList();
            this.lvProduct.DataBind();
        }

        protected void lvProduct_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            Label lb = e.Item.FindControl("lbName") as Label;
            if (lb.Text.Length > 25)
            {
                lb.Text = lb.Text.Substring(0, 25) + "...";
            }
        }
    }
}